//
//  XYEmoCollectionCell.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/23.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYEmoCollectionCell: UICollectionViewCell {

    
    //模型属性
    var emotionModel:[XYEmotionModel]?{
        didSet{
            //遍历全部表情按钮.进行隐藏
            for value in emotionButtonArray{
                value.hidden = true
            }
            //对表情模型遍历.有表情的就显示
            for (index,value) in emotionModel!.enumerate(){
                let button = emotionButtonArray[index]
                button.emotion = value
                button.hidden = false
            }
        }
    }
    
    //装表情按钮的数组
    var emotionButtonArray:[XYEmotionCellButton] = [XYEmotionCellButton]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //创建20个按钮
        for _ in 0..<EMOTION_MAX_NUM{
            let button = XYEmotionCellButton()
            //添加按钮点击事件
            button.addTarget(self, action: "didEmotionButton:", forControlEvents: UIControlEvents.TouchUpInside)
            
            button.titleLabel?.font = UIFont.systemFontOfSize(34)
            
            contentView.addSubview(button)
            emotionButtonArray.append(button)
        }
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //按钮的点击事件
    @objc private func didEmotionButton(sender:XYEmotionCellButton){
        
        let emotion = sender.emotion!
        XYEmotionTool.saveClickButtonEmotion(emotion)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let margin:CGFloat = 30
        
        let chlidButtonW = self.width / CGFloat(EMOTION_COL_MAX_NUM)
        let chlidButtonH = (self.height - margin) / CGFloat(EMOTION_ROW_MAX_NUM)
        
        for (index,value) in emotionButtonArray.enumerate(){
            value.size = CGSizeMake(chlidButtonW, chlidButtonH)
            
            let row = index / EMOTION_COL_MAX_NUM
            let col = index %  EMOTION_COL_MAX_NUM
            value.x = CGFloat(col) * chlidButtonW
            value.y = CGFloat(row) * chlidButtonH
        }
        
    }
    
    
}
